![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
subscription
Advanced tools
A subscription is an object that emits events. Rather than overloading
objects with other roles to emit events, and having a single object
emit several types, as in .addEventListener
style events, a
subscription is a dedicated object representing a single event source.
// Instead of adding listeners by string type
object.addEventListener("message", m => { ... })
// You create a subscription object when you construct the base
// object...
class MyObject {
constructor() {
this.message = new Subscription
...
}
}
// ...and add handlers directly to that:
myObject.message.add(m => { ... })
The interface of a subscription is roughly based on
js-signals
, but
whereas js-signals
have a fixed dispatch algorithm, subscriptions
come in several forms.
The perceived benefits of subscriptions are that they are easier to check statically (instead of a single method dispatching on a bunch of strings, you have actual objects representing your event types, each of which expects a single type of handler function), and lead to more explicit code.
This module is licensed under an MIT license. The recommended way to install it is with NPM.
Subscription
A subscription is an object that you can add subscribers (functions) to, which will be called every time the subscription is dispatched.
add
(f: Function, priority: ?number)
Add a function of the appropriate type for this subscription to be
called whenever the subscription is dispatched. When priority
is
provided (default is zero), it determines when the function is called
relative to other handlers—those with a high priority come first.
addOnce
(f: Function, priority: ?number)
Add a function to be called once, the next time this subscription is dispatched.
remove
(f: Function)
Remove the given subscriber from the subscription.
hasHandler
() → bool
Returns true if there are any functions registered with this subscription.
dispatch
(...args: [any])
Call all handlers for this subscription with the given arguments.
PipelineSubscription
extends Subscription
A type of subscription that passes the value it is dispatched with through all its subscribers, feeding the return value from each subcriber to the next one, and finally returning the result.
dispatch
(value: any) → any
Run all handlers on the given value, returning the result.
StoppableSubscription
extends Subscription
A stoppable subscription is a subscription that stops calling further subscribers as soon as a subscriber returns a truthy value.
dispatch
(...args: [any]) → any
Call handlers with the given arguments. When one of them returns a truthy value, immediately return that value.
DOMSubscription
extends Subscription
A DOM subscription can be used to register intermediate handlers for
DOM events. It will call subscribers until one of them returns a
truthy value or calls preventDefault
on the DOM event.
dispatch
(event: Event) → bool
Run handlers on the given DOM event until one of them returns a truty value or prevents the event's default behavior. Returns true if the event was handled.
FAQs
Simple abstraction for first-class event emitters
The npm package subscription receives a total of 3,513 weekly downloads. As such, subscription popularity was classified as popular.
We found that subscription demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.